﻿
using VaccineHost.Extend;
using VaccineHost.Model;
using VaccineHost.Services;

namespace VaccineHost.Services
{
    /// <summary>
    /// 预约接种记录服务
    /// </summary>
    public class VaccinationService: IVaccinationService
    {
        private readonly HttpClient _httpClient;

        public VaccinationService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        /// <summary>
        /// 根据用户获取预约接种记录
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<PaginatedResult<VaccinationDto>> GetVaccinations()
        {
            VaccinationPageInput pageInput = new VaccinationPageInput();
            pageInput.PageSize = 10000;
            var request = new HttpRequestMessage(HttpMethod.Get, "api/v1/vaccination/info/list")
            {
                Content = JsonContent.Create(pageInput)
            };

            var response = await _httpClient.SendAsync(request);

            var regresult = await response.ToResult();
            if (regresult == null || regresult is OpResult)
            {
                return new PaginatedResult<VaccinationDto>() { Total = 0 };
            }
            else
            {
                return regresult.ToString().ToObject<PaginatedResult<VaccinationDto>>();
            }
        }


        public async Task<OpResult> Update(UpdateVaccinationInfoInput updateInput)
        {
            var request = new HttpRequestMessage(HttpMethod.Put, "api/v1/vaccination/info")
            {
                Content = JsonContent.Create(updateInput)
            };

            var response = await _httpClient.SendAsync(request);

            var regresult = await response.ToResult();
            if (regresult is OpResult)
            {
                return regresult as OpResult;
            }
            else
            {
                OpResult result = new OpResult();
                result.Code = 200;
                result.Message = "更新成功。";

                return result;
            }

            
        }
    }
}
